package register

import (
	"context"
	"log"
	"time"

	"server-go/api/reward/internal/svc"
	"server-go/api/reward/internal/types"

	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/zeromicro/go-zero/core/logx"
)

type CaptchaLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CaptchaLogic {
	return &CaptchaLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CaptchaLogic) Captcha(req *types.CaptchaInput) (resp *types.CaptchaOutput, err error) {
	resp = new(types.CaptchaOutput)
	if req.Email == "" {
		resp.Status = 400
		resp.Data = "get captcha fail, no email"
		return resp, nil
	}
	res, err := l.svcCtx.ProducerA.SendSync(l.ctx, primitive.NewMessage(l.svcCtx.Config.Rocketmq.TopicA, []byte(req.Email)))
	if err != nil {
		return resp, err
	}
	nowStr := time.Now().Format("2006-01-02 15:04:05")
	log.Printf("%s: 消息: %s发送成功, 参数为: %v \n", nowStr, res.String(), req.Email)
	resp.Status = 200
	resp.Data = "get captcha success"
	return resp, nil
}
